﻿//
// Emulate CMD shell 'tree /a' command: Graphically displays the folder structure of a drive or path.
// Usage:
//   TREECON [drive:][path]
//

using System;
using System.IO;
using System.Management;

using FileTreeSpace;


namespace ConTreeSpace
{
    class ConTree
    {
        static void Main (string[] args)
        {
            DirRoot root = null;
            string dirArg = args.Length > 0 ? args[0] : @"c:\bin";
            string dirName = Path.GetFullPath (dirArg);

            if (dirName.Length < 2 || dirName[1] != ':')
            {
                Console.WriteLine ("Invalid drive specification");
                return;
            }

            try
            {
                root = new DirRoot (dirName);
            }
            catch (ArgumentException)
            {
                Console.Write ("Invalid path - ");
                Console.Write (dirName.ToUpper ());
                Console.WriteLine ();
                return;
            }


            DriveInfo[] dr = DriveInfo.GetDrives ();

            foreach (DriveInfo d in dr)
            {
                if (d.IsReady)
                {
                    if (d.Name[0] == dirName.ToUpper ()[0])
                    {
                        Console.Write ("Folder PATH listing for volume ");
                        Console.Write (d.VolumeLabel);
                        break;
                    }
                }
            }
            Console.WriteLine ();

            foreach (DirPath path in root)
            {
                if (path.Count == 1)
                    Console.WriteLine ("{0}", path.NodeName.ToUpper ());
                else
                {
                    int k = 1;
                    while (k < path.Count - 1)
                    {
                        // Output the appropriate indentation.
                        if (path.HasMore (k))
                            Console.Write (@"|   ");
                        else
                            Console.Write (@"    ");
                        ++k;
                    }

                    if (path.HasMore (k))
                        Console.Write (@"+---");
                    else
                        Console.Write (@"\---");

                    Console.WriteLine ("{0}", path.NodeName);
                }
            }
        }
    }
}
